package org.example.xiaomi.dao.impl;

import org.example.xiaomi.dao.UserDao;
import org.example.xiaomi.entity.User;
import org.example.xiaomi.service.UserService;
import org.example.xiaomi.tx.TransactionManager;
import org.example.xiaomi.vo.ResultVO;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new Sql2oUserDaoImpl();

    @Override
    public ResultVO findUserByNameAndPassword(String username, String password) {
        try {
            TransactionManager.beginTransaction();
            User user = userDao.findUserByName(username);

            if (user == null) {
                return ResultVO.fail("用户不存在");
            }
            if (user != null && !user.getPassword().equals(password)) {
                return ResultVO.fail("用户名或密码不对");
            }
            TransactionManager.commitTransaction();
            return ResultVO.success(user);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionManager.rollbackTransaction();
            return ResultVO.fail(e.getMessage());
        }

    }
}
